//获取参数
var typeData = getQueryString('type');                                            //参数1
var num = 1;                                                                          //参数2
var app = angular.module("myApp",[]);

app.controller("myCtrl",function($scope,$http,getImformation){
	$scope.area = false;
	$scope.thisArea = "北京";
	$scope.areaCode = "";                                                         //参数3
	$scope.checkArea = function(){
		$scope.area = true;
		var params = {
	       parentId : 1
		};
		params = $.param(params);
		$http({
			method : "post",
			dataType: "json",
			withCredentials: true,	//跨域名请求。XHR：XMLHttpRequest (XHR) ，基于XML技术的Http请求
			crossDomain: true,
			headers:{'Content-Type': 'application/x-www-form-urlencoded'},
			data:params,
			async:true,
		    url : host + "/terminal/chinaRegion/list"
			}).success(function(response){
			if(response.code == 2000){
			    $scope.city = response.data.regionArrayList;
			}
	    }).error(function(response){
			console.log(response);
		});
	}
	$scope.areaTitle = function($event){
		$scope.thisArea = $($event.target).text();
		if($($event.target).text() == "新疆维吾尔自治区"){
			$scope.thisArea = "新疆";
		}
		$scope.area = false;
		$scope.areaCode = $($event.target).attr("cityCode");
		
		num = 1; //初始化当前的页码
		getImformation.initImformation(typeData,1,$scope.areaCode,$scope.preTime,$scope.nextTime,function(response){
			$scope.arr = response.data.list;
		})
	}
	
	//选择时间事件
	$scope.preTime = "";                                                              //参数4
	$scope.nextTime = "";                                                             //参数5
	$scope.toShow = false;
	
	$scope.timeShow = function(){
		$scope.toShow = true;
	}
	$scope.timeHide = function(){
		$scope.preTime = $("#preTime").val() + " 00:00:00";
		$scope.nextTime = $("#nextTime").val() + " 00:00:00";
		
		//当用户没有输入任何值时，默认为空
		if($scope.preTime == " 00:00:00"){
			$scope.preTime = "";
		}
		if($scope.nextTime == " 00:00:00"){
			$scope.nextTime = "";
		}
		
		num = 1; //初始化当前的页码
		getImformation.initImformation(typeData,1,$scope.areaCode,$scope.preTime,$scope.nextTime,function(response){
			$scope.arr = response.data.list;
		})
		
		$scope.toShow = false;
	}
	
	//存储数据
	$scope.arr = [];
	//$scope.arrAll = [];
	
	//获取数据
	getImformation.initImformation(typeData,1,$scope.areaCode,$scope.preTime,$scope.nextTime,function(response){
		$scope.arr = response.data.list;
	})
	
	//angular的监听事件
    window.addEventListener("scroll", function(event) {
    	var clientHeight = $(this).height();
    	var scrollTop = $(this).scrollTop();
    	var docHeight = $(document).height();
    	//判断是否到达底部，上拉加载
        if(clientHeight + scrollTop >= docHeight){
        	num++;
        	getImformation.initImformation(typeData,num,$scope.areaCode,$scope.preTime,$scope.nextTime,function(response){
        		if(num > response.data.pager.pageCount){
					return;
				}
				$scope.arr = $scope.arr.concat(response.data.list);				
			})
	    }    
    });
	
})

//创建ng的工厂,请求信息的数据，进行依赖注入
app.factory("getImformation",function($http){
	var obj = {};
	obj.initImformation = function(x,y,z,t,e,_callback){
		var params = {
		    	type : x,  //类名
		    	pageNum : y,
		    	pageSize : 10,
		    	areaCode : z,
		    	startTime : t,
		    	endTime : e
		    };
		params = $.param(params);
		$http({
	        method : "post",
	        dataType: "json",
			withCredentials: true,	//跨域名请求。XHR：XMLHttpRequest (XHR) ，基于XML技术的Http请求
		    crossDomain: true,
		    headers:{'Content-Type': 'application/x-www-form-urlencoded'},
		    data:params,
	        //async:true,
	        url : host + "/terminal/emergencyInformation/infoArticle"
	   }).success(function(response){
	        if(response.code == 2000){
	        	_callback(response);
	        }
	    }).error(function(response){
	    	console.log(response);
	    });
	}
	return obj;
})
